package com.Rolin.wss.controller;

import com.Rolin.wss.model.UserAuthVO;
import com.Rolin.wss.model.UserRegisterVO;
import com.Rolin.wss.model.UserVO;
import com.Rolin.wss.service.UserService;
import com.Rolin.wss.util.utils;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;


/**
 * 该控制器负责处理用户请求
 * 包括登陆注册登出等
 * 此处处理的域为/wss/user
 * 即处理请求的URL为：/wss/user/login、/wss/user/register等
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
    //引入日志
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //注入Service
    @Autowired
    UserService userService;

    /**
     * 用户进入登录界面
     * 若用户已登录则回跳至首页，若没登陆则进入登陆界面
     * @param session 用户登陆完会将数据存放至Session
     * @param model 视图model
     * @return 返回登陆界面视图或者首页视图
     */
    @RequestMapping(value = "/login")
    public String login(HttpSession session, Model model){
        UserVO user = (UserVO) session.getAttribute("user");
        if(user == null) return "login";
        else return "index";
    }

    /**
     * 1.先校验信息的格式(还没做)
     * 2.再校验信息准确度
     * 3.再写入Session
     * 处理用户登录请求，包括校验信息与写入Session等
     * @param userAuth 用户校验信息对象，包括一个电话号码和一个密码
     * @param session 服务器Session对象，用于把登陆成功的用户写入Session
     * @return 返回一个JSON字符串给前端对象
     */
    @RequestMapping(value = "/login.do",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String verifyLoginInformation(@RequestBody UserAuthVO userAuth,HttpSession session){
        int verifyResult = userService.verifyUser(userAuth); //验证信息 返回-1：查询错误 返回其他：用户id

        //表查询错误
        if (verifyResult == -1) return utils.buildJSONForReturn(1,"登陆失败,请检查用户名和密码是否正确");

        //verifyResult为用户ID
        int userID = verifyResult;
        UserVO user = userService.getUser(userID);

        if(user == null) {
            logger.error("Unable to get UserVO object by ID!");
            return utils.buildJSONForReturn(1,"登陆失败,查找不到该用户");
        }

        session.setAttribute("user",user);
        return utils.buildJSONForReturn(0,"登陆成功,用户ID为："+userID);
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout(HttpSession session, Model model){
        UserVO user = (UserVO)session.getAttribute("user");
        if(user == null) {
            model.addAttribute("userCome","<a class=\"come\" href=\"/wss/user/login\">登录/注册</a>");
            return "index";
        }
        session.removeAttribute("user");
        session.removeAttribute("Ticket");
        model.addAttribute("userCome","<a class=\"come\" href=\"/wss/user/login\">登录/注册</a>");

        return "index";
    }

    @RequestMapping(value = "/register.do",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String register(@RequestBody UserRegisterVO user,Model model){
        if(userService.registerUser(user)){
            return utils.buildJSONForReturn(0,"注册成功");
        }
        return utils.buildJSONForReturn(1,"注册失败，请检查用户名");
    }
}
